package org.jimmy.tang.lang;

/**
 * @author jimmy
 * 
 */
public class CloneDemo {
    public static void main(String... args) throws CloneNotSupportedException {
        Employee7 a = new Employee7("Lily", 1000);

        Employee7 staff = a;// a与staff同时引用同一个对象
        a.setName("Bob");
        System.out.println(a);// com.xujin.Employee7[name = Bob, salary =
                              // 1000.0]
        System.out.println(staff); // com.xujin.Employee7[name = Bob, salary =
                                   // 1000.0]

        Employee7 copy = a.clone();
        a.setName("Jim");
        a.setSalary(2000);
        System.out.println(a);// com.xujin.Employee7[name = Jim, salary =
                              // 2000.0]
        System.out.println(copy);// com.xujin.Employee7[name = Bob, salary =
                                 // 1000.0]
    }
}

class Employee7 implements Cloneable {
    public Employee7(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return getClass().getName() + "[name = " + name + ", salary = "
                + salary + "]";
    }

    @Override
    public Employee7 clone() throws CloneNotSupportedException {
        return (Employee7) super.clone();
    }

    // 定义变量
    private double salary;
    private String name;
}
